#include "UIMusicInfoPanel.h"
#include "AppDelegate.h"

UIMusicInfoPanel * UIMusicInfoPanel::create(Size disparea, float tabwidth)
{
	auto mip = new UIMusicInfoPanel();
	mip->init();

	mip->disparea = disparea;
	mip->tabwidth = tabwidth;
	mip->pagearea = Size(disparea.width - tabwidth, disparea.height);

	auto bg = LayerGradient::create(THEMECOLORBA(255), THEMECOLORBA(128), Vec2(-1, 0));
	bg->changeWidthAndHeight(disparea.width, disparea.height);
	mip->addChild(bg);
	mip->setStencil(bg);

	mip->tablayer = LayerColor::create(THEMECOLORB, tabwidth, disparea.height);
	mip->tablayer->setPositionX(mip->pagearea.width);
	mip->addChild(mip->tablayer);

	return mip;
}

UIMusicInfoPanelPage* UIMusicInfoPanel::addTabPage(string name, int colcount, int rowcount, bool scrollable)
{
	auto tabpage = UIMusicInfoPanelPage::create(pagearea, colcount, rowcount, scrollable);
	auto tabbtn = UICButton::create(Size(tabwidth*.8f, disparea.height*.2f), name, THEMECOLORBA(0), TEXTCOLORB);
	tabbtn->setTag(tabcount);
	tabbtn->setDirection(UICButton::DIRECTION::RIGHT);
	tabbtn->setPositionY((.8f - tabcount*.21f)*disparea.height);

	addChild(tabpage);
	tablayer->addChild(tabbtn);

	tabpages.push_back(tabpage);
	tabbtns.push_back(tabbtn);
	tabcount++;
	return tabpage;
}

void UIMusicInfoPanel::setTabButtonActive(int index)
{
	for (auto btn : tabbtns)
	{
		btn->setActiveStat(false);
	}
	tabbtns[index]->setActiveStat(true);
}

UIMusicInfoPanelPage * UIMusicInfoPanelPage::create(Size disparea, int colcount, int rowcount, bool scrollable)
{
	auto mipp = new UIMusicInfoPanelPage();
	mipp->initWithColor(THEMECOLORBA(0));

	mipp->disparea = disparea;
	mipp->colcount = colcount;
	mipp->rowcount = rowcount;
	mipp->scrollable = scrollable;
	mipp->itemwidth = disparea.width / colcount;
	mipp->itemheight = disparea.height / rowcount;

	return mipp;
}

void UIMusicInfoPanelPage::addItem(Node * item, int col, int row)
{
	addChild(item);
	item->setPosition((colcount - col)*itemwidth, (rowcount - row)*itemheight);
}
